local lightningTriSkill = fk.CreateSkill {
  name = "lightningTriSkill",
  tags = { Skill.Compulsory, },
}

lightningTriSkill:addEffect(fk.TurnStart, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(lightningTriSkill.name) and player:getMark("@lightning_num") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player,"@lightning_num",1)
    -- room:notifySkillInvoked(player, lightningTriSkill.name, "negative")
    local mark = player:getMark("@lightning_num")
    if mark <= 0 then
      room:damage{
        to = player,
        damage = 3,
        damageType = fk.ThunderDamage,
        skillName = lightningTriSkill.name,
      }
      room:handleAddLoseSkills(player, "-lightningTriSkill", nil, false)
      local card = table.find(player.player_cards[Player.Judge],function(cid) return Fk:getCardById(cid).name == "wdtx__lightning" end)
      -- card = Fk:getCardById(card)
      if not card then return end
      room:moveCards{
        from = player,
        fromArea = Card.PlayerJudge,
        ids = {card},
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonJustMove
      }
    end
  end,
})

lightningTriSkill:addEffect(fk.BeforeCardsMove, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(lightningTriSkill.name) then return end
    for _, move in ipairs(data) do
      if move.from and move.from == player and move.toArea ~= Card.Processing then
        -- p(data)
        -- dbg()
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card.name == "wdtx__lightning" and info.fromArea == Card.PlayerJudge then
            event:setCostData(self, info.cardId)
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player, "-lightningTriSkill", nil, false)
    room:setPlayerMark(player,"@lightning_num",0)
  end,
})


return lightningTriSkill